package com.songlanyun.modules.order.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.OrderException;
import com.songlanyun.modules.order.entity.ChildOrder;
import com.songlanyun.modules.order.entity.SaleServiceGoodsCard;
import com.songlanyun.modules.order.entity.SaleServiceOrderContacts;
import com.songlanyun.modules.order.model.dto.ReservationRecordDTO;
import com.songlanyun.modules.order.service.*;
import com.songlanyun.modules.servicegoods.entity.ServiceGoods;
import com.songlanyun.modules.servicegoods.service.ServiceGoodsService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.order.dao.ReservationRecordDao;
import com.songlanyun.modules.order.entity.ReservationRecordEntity;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;


@Service("reservationRecordService")
public class ReservationRecordServiceImpl extends ServiceImpl<ReservationRecordDao, ReservationRecordEntity> implements ReservationRecordService {


    @Resource
    private ServiceGoodsService serviceGoodsService;

    @Resource
    private ChildOrderService childOrderService;

    @Resource
    private SaleServiceGoodsCardService saleServiceGoodsCardService;

    @Resource
    private SaleServiceGoodsVerificationRecordService saleServiceGoodsVerificationRecordService;

    @Resource
    private SaleServiceOrderContactsService saleServiceOrderContactsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long shopId = MapUtil.getLong(params, "shop_id");
        Long userId = MapUtil.getLong(params, "user_id");
        Long serviceGoodsId = MapUtil.getLong(params, "service_goods_id");
        Long orderId = MapUtil.getLong(params, "order_id");
        Long childOrderId = MapUtil.getLong(params, "child_order_id");
        String orderCode = MapUtil.getStr(params, "order_code");
        String date = MapUtil.getStr(params, "date");

        IPage<ReservationRecordEntity> page = this.page(
                new Query<ReservationRecordEntity>().getPage(params),
                new LambdaQueryWrapper<ReservationRecordEntity>()
                        .eq(ReservationRecordEntity::getShopId, shopId)
                        .eq(userId != null, ReservationRecordEntity::getUserId, userId)
                        .eq(serviceGoodsId != null, ReservationRecordEntity::getServiceGoodsId, serviceGoodsId)
                        .eq(orderId != null, ReservationRecordEntity::getOrderId, orderId)
                        .eq(childOrderId != null, ReservationRecordEntity::getChildOrderId, childOrderId)
                        .like(orderCode != null, ReservationRecordEntity::getOrderCode, orderCode)
                        .like(date != null, ReservationRecordEntity::getAppointmentTime, date)
                        .orderByDesc(ReservationRecordEntity::getCreateTime)
        );

        return new PageUtils(page);
    }


    @Override
    public List<ReservationRecordEntity> myList(Long userId) {
        return this.baseMapper.selectList(
                new LambdaQueryWrapper<ReservationRecordEntity>()
                        .eq(ReservationRecordEntity::getUserId, userId)
                        .orderByDesc(ReservationRecordEntity::getCreateTime)
        );
    }

    @Override
    @Transactional
    public void forward(ReservationRecordDTO dto) {

        //核销
        SaleServiceGoodsCard saleServiceGoodsCard = saleServiceGoodsCardService.loadByCode(dto.getVerificationCode(), true);
        //服务订单核销码 和 该服务订单 不匹配
        if (!saleServiceGoodsCard.getChildOrderId().equals(dto.getChildOrderId())) {
            throw new RRException(OrderException.SERVICE_CARD_ORDER_NOT_SAME);
        }

        //子订单
        ChildOrder childOrder = childOrderService.getById(dto.getChildOrderId());

        //校验
        saleServiceGoodsVerificationRecordService.verifyServiceOrder(dto.getShopId(), saleServiceGoodsCard, childOrder);

        //保存预约记录
        saveRecord(dto, childOrder);
    }

    private void saveRecord(ReservationRecordDTO dto, ChildOrder childOrder) {
        ReservationRecordEntity record = new ReservationRecordEntity();
        BeanUtil.copyPropertiesIgnoreNull(dto, record);

        record.setServiceGoodsId(childOrder.getGoodsId());
        record.setUserId(childOrder.getUserId());

        SaleServiceOrderContacts saleServiceOrderContacts = saleServiceOrderContactsService.loadByOrderId(childOrder.getOrderId());

        record.setMobile(saleServiceOrderContacts.getMobile());
        record.setNickname(saleServiceOrderContacts.getContacts());
        record.setOrderId(childOrder.getOrderId());
        record.setChildOrderId(childOrder.getId());
        record.setOrderCode(childOrder.getOrderCode());

        this.baseMapper.insert(record);
    }


}
